<template>
	<view class="pages">
		<view class="cards">
			<view class="avator">
				<!-- <image :src="imgUrl" mode="scaleToFill" /> -->
				 <u-swiper
				      :list="posterList"
				      previousMargin="30"
				      nextMargin="30"
				      circular
					  :current="current"
					  :height="534"
					  imgMode="scaleToFill"
				      :autoplay="false"
				      radius="5"
					  @change="posterChange"
				    ></u-swiper>
			</view>
		</view>
		<view class="btnsave" @click="save">
			<image src="https://pw.qyang.cc/static/cave.png" mode="scaleToFill" />
			<p>保存图片</p>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl:'',
				current:0,
				posterList:[]
			};
		},
		onLoad() {
			uni.showLoading()
			this.rq.getData('daren/make_poster').then(res => {
				uni.hideLoading()
				if(typeof(res.data)=='string'){
					this.posterList = ['data:image/jpg;base64,'+ res.data]
				}else{
					this.posterList = res.data.map(item =>{
						return 'data:image/jpg;base64,'+ item
					})
				}
				this.imgUrl = this.posterList[this.current].split('base64,')[1]
			}).catch(err=>{
				uni.hideLoading()
			})
		},
		methods: {
			posterChange(e){
				this.current = e.current
				this.imgUrl = this.posterList[e.current].split('base64,')[1]
			},
			save() {
				let _this = this
				//#ifdef MP-WEIXIN
				uni.getFileSystemManager().writeFile({
					filePath: wx.env.USER_DATA_PATH + '/陪玩推广名片.jpg',
					data: _this.imgUrl,
					encoding: 'base64',
					success: res => {
						console.log(res)
						uni.saveImageToPhotosAlbum({
							filePath: wx.env.USER_DATA_PATH + '/陪玩推广名片.jpg',
							success: function() {
								//保存成功
								uni.showToast({
									title:'保存成功',
									icon:'success'
								})
							},
							fail: function() {
								//保存失败
								uni.showToast({
									title:'保存失败',
									icon:'success'
								})
							}
						})
					},
					fail: err => {
						console.log(err)
					}
				})
				//#endif
				// #ifndef MP-WEIXIN
				const bitmap = new plus.nativeObj.Bitmap('base64')
				bitmap.loadBase64Data('data:image/png;base64,'+ _this.imgUrl,()=>{
					const url = '_doc/'+ new Date().getTime() + '.png'
					bitmap.save(url,{overweite:true},(i)=>{
						uni.saveImageToPhotosAlbum({
							filePath: url,
							success: function() {
								//保存成功
								uni.showToast({
									title:'保存成功',
									icon:'success'
								})
								bitmap.clear()
							},
							fail: function(err) {
								console.log(err)
								//保存失败
								uni.showToast({
									title:'保存失败',
									icon:'success'
								})
								bitmap.clear()
							}
						})
					})
				})
				
				// #endif
			},
		},
	};
</script>

<style lang="scss" scoped>
	.pages {
		background-color: #f6f6f6;
		padding-bottom: 40rpx;
		.cards {
			position: relative;
			// width: 600rpx;
			min-height: 70vh;
			// background: #ffffff;
			margin: 80rpx auto;
			border-radius: 25rpx;

			.avator {
				image {
					width: 538rpx;
					height: 1068rpx;
				}
			}

			.ewm {
				position: absolute;
				bottom: 25%;
				left: 30%;
				background: #fff;
				border-radius: 25rpx;

				image {
					height: 230rpx;
					width: 230rpx;
				}
			}

			.info {
				display: flex;
				padding: 30rpx;
				align-items: center;

				.avaimg {
					image {
						width: 90rpx;
						height: 90rpx;
						margin-right: 15rpx;
					}
				}
			}
		}

		.btnsave {
			display: flex;
			align-items: center;
			background-color: #ff5866;
			color: #ffffff;
			width: 700rpx;
			margin: 0 auto;
			border-radius: 50rpx;
			padding: 25rpx 0;
			justify-content: center;

			image {
				width: 30rpx;
				height: 30rpx;
				margin-right: 20rpx;
			}
		}
	}
</style>